package org.jeecgframework.web.material.controller;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.material.entity.CustomerEntity;
import org.jeecgframework.web.material.service.CustomerServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Description: customer
 * @date 2015-08-17 10:01:47
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/customerController")
public class CustomerController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CustomerController.class);

	@Autowired
	private CustomerServiceI customerService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "customer")
	public ModelAndView customer(HttpServletRequest request) {
		return new ModelAndView("material/customer/customerList");
	}
	
	/**
	 * customer新增或编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAddOrUpdate")
	public ModelAndView goAdd(CustomerEntity customer, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(customer.getId())) {
			customer = customerService.getEntity(CustomerEntity.class, customer.getId());
			req.setAttribute("customerPage", customer);
		}
		return new ModelAndView("material/customer/customer");
	}

	/**
	 * 客户列表
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(CustomerEntity customer,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(CustomerEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, customer, request.getParameterMap());
		try{
		//自定义追加查询条件
			TSUser user = ResourceUtil.getSessionUserName();
			cq.createAlias("company", "company");
			cq.eq("company.id", user.getCompany().getId());
			//按时间降序排序
			cq.addOrder("createDate", SortDirection.desc);
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.customerService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 添加customer
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAddOrUpdate")
	@ResponseBody
	public AjaxJson doAdd(CustomerEntity customer, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if(StringUtil.isEmpty(customer.getId())){
			message = "客户添加成功";
			try{
				customer.setUserName(customer.getMobilePhone());
				customer.setPassword(customer.getMobilePhone());
				TSUser user = ResourceUtil.getSessionUserName();
				customer.setCreateDate(new Date());
				customer.setCompany(user.getCompany());
				customerService.save(customer);
				systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
			}catch(Exception e){
				e.printStackTrace();
				message = "客户添加失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		}else{
			CustomerEntity t = customerService.get(CustomerEntity.class, customer.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(customer, t);
				customerService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "客户更新失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		}
		
		return j;
	}
	

	/**
	 * 删除customer
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(CustomerEntity customer, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		customer = systemService.getEntity(CustomerEntity.class, customer.getId());
		message = "客户删除成功";
		try{
			customerService.delete(customer);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "客户删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除customer
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "客户删除成功";
		try{
			for(String id:ids.split(",")){
				CustomerEntity customer = systemService.getEntity(CustomerEntity.class, 
				id
				);
				customerService.delete(customer);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "客户删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

}
